package com.example.zsfinacialmanage.activity;

import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.example.zsfinacialmanage.R;
import com.example.zsfinacialmanage.constant.MessageConstant;
import com.example.zsfinacialmanage.utils.MyDBHelper;
import com.example.zsfinacialmanage.view.MainActivity;

public class NewPayActivity extends AppCompatActivity {

    //定义对象
    EditText et_money,et_time,et_payer,et_remark;
    Spinner sp_type;
    Button bt_save,bt_cancel;
    MyDBHelper myDBHelper;
    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_pay);
        initView();
        btnSave();
        btnCancel();
    }

    private void initView() {
        et_money = findViewById(R.id.et_money_newout);
        et_time = findViewById(R.id.et_time_newout);
        sp_type = findViewById(R.id.sp_type_newout);
        et_payer = findViewById(R.id.et_payer_newout);
        et_remark = findViewById(R.id.et_remark_newout);
        bt_save = findViewById(R.id.bt_save_newout);
        bt_cancel = findViewById(R.id.bt_cancel_newout);
        myDBHelper = new MyDBHelper(NewPayActivity.this);
        db = myDBHelper.getWritableDatabase();
    }

    private void btnSave() {
        bt_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String myName = getSharedPreferences("userinfo",0).getString("userName","");
                //将用户输入的内容保存到数据库
                ContentValues values = new ContentValues();
                values.put("name",myName);
                values.put("outmoney",et_money.getText().toString());
                values.put("outtime",et_time.getText().toString());
                values.put("outtype",sp_type.getSelectedItem().toString());
                values.put("outpayee",et_payer.getText().toString());
                values.put("outremark",et_remark.getText().toString());
                db.insert("pay_out",null,values);
                Toast.makeText(NewPayActivity.this, MessageConstant.SAVE_SUCCESS,Toast.LENGTH_SHORT).show();
                //刷新页面
                Intent intent =new Intent(NewPayActivity.this, NewInComeActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    private void btnCancel() {
        bt_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(NewPayActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }
}